
<script type="text/javascript">
	$(document).ready(function(){
		$('#petition').dataTable();
	});
	function printCheque(month, year){
		var val = [];
		var count=0;


		
		$(':checkbox:checked').each(function(i){
		  val[i] = $(this).val();
		  count++;
		  //alert(val[i]);
		});

		if (count == 0) {
			alert ("กรุณาเลือกข้อมูลที่ต้องการพิมพ์ด้วยครับ");
		} else {
			$('<div>').dialog2({
				autoOpen: true,
				title: "พิมพ์เช็ค",
				content: "dlg_prt_cheque_salary.php?val=" + val + "&month=" + month + "&year=" + year,
				id: "modal-printCheque"
			});
		}
	}
</script>

<?php
	/*
	$sql ="SELECT DISTINCT tbl_salary.pet_id, expenses1_name, expenses2_name, expenses3_name, panngan2_name, panngan3_name, sum(salary) as salary, cheque_no, month_year FROM tbl_salary, expenses_lvl1, expenses_lvl2, expenses_lvl3, panngan2, panngan3  WHERE lvl1_id = '" .$_SESSION["lvl1_id"]. "' and expenses_lvl1.expenses1_id = ex1_id and expenses_lvl2.expenses2_id = ex2_id and expenses_lvl3.expenses3_id = ex3_id and panngan2.panngan2_id = tbl_salary.panngan2_id and panngan3.panngan3_id = tbl_salary.panngan3_id and approved_status = 'อนุมัติ'
	GROUP BY tbl_salary.pet_id, expenses1_name, expenses2_name, expenses3_name, panngan2_name, panngan3_name, cheque_no, month_year
	";
	*/

	$month = $_REQUEST["month"];
	$year = $_REQUEST["year"];

	$sql ="SELECT pet_id, id, salary - exp_total as salary, month_year, cheque_no
			FROM tbl_salary
			WHERE lvl1_id = '" .$_SESSION["lvl1_id"]. "' and approved_status =  'อนุมัติ' and month_year = '$year-$month'
			";

	$db = new myDBS;
	$db->selectDB("account");
	$result = $db->runSQL($sql);

?>
<form method="get" action="index.php" class="form-inline">
	<input type="hidden" name="pages" value="<?php echo $pages; ?>">
	<select name="month">
		<?php
			$array_month = array("01" => "มกราคม", "02" => "กุมภาพันธ์", "03" => "มีนาคม", "04" => "เมษายน", "05" => "พฤษภาคม", "06" => "มิถุนายน", "07" => "กรกฎาคม", "08" => "สิงหาคม", "09" => "กันยายน", "10" => "ตุลาคม", "11" => "พฤศจิกายน", "12" => "ธันวาคม");
			
			foreach ($array_month as $key => $value) {
				if ($key == $month) {
					echo "<option value='$key' selected>$value</option>";
				} else {
					echo "<option value='$key'>$value</option>";
				}
			}
		
		?>
	</select>
	<select name="year">
		<?php
			$y = date("Y");

			if (is_null($year) || !isset($year) || empty($year)) {
				$year = date("Y");
			}
			for ($i = $y - 5; $i < $y + 5; $i++ ) {
				if ($i == $year) {
					echo "<option value='$i' selected>$i</option>";
				} else {
					echo "<option value='$i'>$i</option>";
				}
			}
		?>
		
	</select>
	<input type="submit" class = "btn btn-info" value="ตกลง">
</form>
<table id="petition" class="table table-striped table-bordered" width="100%" >
	<caption><br></caption>
	<thead>
		<tr>
			<th>#</th>
			<th>เลขที่ฏีกา</th>
			<th>เลขที่บัตรประชาชน</th>
			<th>ชื่อ - นามสกุล</th>
			<th>ประจำเดือน</th>
			<th>จำนวนเงิน</th>
			<th>เลือก</th>
			<th>เลขที่เช็ค</th>

		</tr>
	</thead>
	<tbody>
	<?php
		$i=0;
		while ( $rows = $db->fetch_object ( $result )){
			$i++;
			$sql ="SELECT fname, lname
						FROM person
						WHERE id = '". $rows->id ."'
						";

				$db1 = new myDBS;
				$db1->selectDB("hr_udoncity");
				$result1 = $db1->runSQL($sql);
				$rows1=$db1->fetch_object ( $result1 );

			echo "<tr>
					<td>$i</td>
					<td>$rows->pet_id</td>
					<td>$rows->id</td>
					<td>$rows1->fname&nbsp;&nbsp;&nbsp;$rows1->lname</td>
					<td>$rows->month_year</td>
					<td>". number_format($rows->salary, 2)."</td>

			";
			
			if ($rows->cheque_no == "" || is_null($rows->cheque_no) || empty($rows->cheque_no)) {
				echo "<td><input type='checkbox' name='check[]' id='check_$rows->id' value='$rows->id' /></td>
					<td>&nbsp;</td>
				";
			} else {
				echo "<td><a href='prt_cheque_salary.php?cheque_no=$rows->cheque_no' data-toggle='tooltip' title='พิมพ์' target='_blank'><i class='icon-print'></i></a></td>";
				echo "<td>$rows->cheque_no</td>";
			}
			
			echo "
					</tr>
			";

		}
	?>
	</tbody>
</table>
<a href="javascript:printCheque('<?php echo $month; ?>', '<?php echo $year; ?>')" role="button" class="btn btn-large btn-primary" target="_blank"><i class="icon-print icon-white"></i> ตกลง</a>